package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.TypeOneViewHolder;
import where2go.sctu.com.zhiyou.entity.PersonRecomClass;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.adpater
 * Created by 张某人
 * CreatedTime:on 2018/5/1   21:20
 * Description:
 */

public class RecyclerViewOneAdapter extends RecyclerView.Adapter<TypeOneViewHolder> {

    //private LayoutInflater mLayoutInflater;

    //课程图片路径
    private String urlPersonal="http://47.104.80.155/youyiyou/resources/classImg/";
    private List<PersonRecomClass.DataBean> mList=new ArrayList<>();
    private Context context;

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener=listener;
    }

    public RecyclerViewOneAdapter(List<PersonRecomClass.DataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    public void addList(List<PersonRecomClass.DataBean> list){
        mList.addAll(list);
    }


    @Override
    public TypeOneViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_type_one,parent,false);
        TypeOneViewHolder holder=new TypeOneViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(final TypeOneViewHolder holder, final int position) {
        PersonRecomClass.DataBean personBean= mList.get(position);
        holder.name.setText(personBean.getClassTitle());
        ImageRequest imageRequest = new ImageRequest(urlPersonal+mList.get(position).getClassImg(),
                new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                holder.avatar.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片
                holder.avatar.setImageResource(R.mipmap.ic_launcher);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);

        if (mOnItemClickListener!=null){
             holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });

             holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                 @Override
                 public boolean onLongClick(View v) {
                     mOnItemClickListener.onItemLongClick(holder.itemView,position);
                     return false;
                 }
             });
        }

    }
    @Override
    public int getItemCount() {
        return mList !=null ? mList.size():0;
    }
}
